<template>
  <!-- 新增权限 -->
  <el-dialog :title="title +'权限'" :visible="isShowDialog" @close="closeFn">
    <!-- 表单 -->
    <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="权限名称" prop="title">
        <el-input v-model="formData.title" style="width:90%" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="formData.code" style="width:90%" />
      </el-form-item>
      <el-form-item label="权限描述" prop="description">
        <el-input v-model="formData.description" style="width:90%" />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" type="primary" @click="confirmFn">确定</el-button>
        <el-button size="small" @click="closeFn">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { addPermissions, getPermissionsById, EditPermissions } from '@/api/permissions'
export default {
  props: {
    isShowDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        title: '',
        code: '',
        description: ''
      },
      rules: {
        title: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '权限名称2到10个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '权限标识2到10个字符', trigger: 'blur' }
        ],

        description: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '权限标识2到10个字符', trigger: 'blur' }
        ]
      },
      title: '新增',
      config: {
        pid: '0',
        type: '1'
      }
    }
  },
  methods: {
    // 关闭弹窗
    closeFn() {
      // 重置数据
      this.formData = {
        title: '',
        code: '',
        description: ''
      }
      this.$emit('update:isShowDialog', false)
      // 重置表单
      this.$refs.perForm.resetFields()
    },
    // 弹窗确认事件
    async confirmFn() {
      try {
        // 验证
        await this.$refs.perForm.validate()
        if (this.formData.id) {
          // 编辑
          await EditPermissions(this.formData)
          this.$message.success('修改权限成功')
        } else {
          // 新增
          this.formData = {
            ...this.formData,
            ...this.config
          }
          // console.log(this.formData)
          // 调用新增接口
          await addPermissions(this.formData)
          this.$message.success('新增权限成功')
        }
        // 关闭弹窗
        this.$emit('update:isShowDialog', false)
        // 刷新
        this.$emit('updateEvent')
      } catch (err) {
        console.log(err)
      }
    },
    // 根据id获取权限详情
    async getPermissionsById(id) {
      // console.log(id)
      const res = await getPermissionsById(id)
      this.formData = res.data
      console.log(this.formData)
    }
  }
}
</script>

<style>
</style>
